import Vue from 'vue'

//=========设置路由 ============

import VueRouter from 'vue-router'

import router from './router'

Vue.use(VueRouter)

//=========vuex============

import Vuex from 'vuex'

Vue.use(Vuex)

//本地提取
var car = JSON.parse(localStorage.getItem('car') || '[]')

const store = new Vuex.Store({
    state: {  //this.$store.state.**** , 不可修改数据

      car: car

    }, 
    mutations: {  //this.$store.commit('方法名',参数)

      getToCar(state, goodsinfo){  //将goodsinfo 加入到store中，计算购物数量count

        var carflag = false

          state.car.some(item =>{  //条件查找用some方法

          if(item.id ===  goodsinfo.id){  

            item.count +=  parseInt(goodsinfo.count) 

            carflag = true

            return true  //查找到后，终止循环

          }

        })

        if(!carflag){

          state.car.push(goodsinfo) //将新的对象加入到car中

        }
        
        //本地存储
        localStorage.setItem("car", JSON.stringify(state.car))

      },

      updateGoodsCount(state, obj){   //更新购物车中的商品数量

        state.car.some(item=>{

            if(item.id == obj.id){

              item.count = obj.count

              return true

            }

        })

        localStorage.setItem("car", JSON.stringify(state.car))

      },

      removeCar(state,id){ //删除商品的数量

        state.car.some((item,i)=>{

          if(item.id == id){

            state.car.splice(i ,1)

            return true

          }

        })

        localStorage.setItem("car", JSON.stringify(state.car))

      },

      updateSelected(state, obj){  //更新选择状态

        state.car.some(item =>{

          if(item.id == obj.id){

           item.selectStatus = obj.selectStatus

            return true

          }

        })

        localStorage.setItem("car", JSON.stringify(state.car))

      }


    },
    getters: {  //this.$store.getters.****

        getAllCount(state){  //计算count的总值

          var c = 0 

          state.car.forEach(item =>{

            c += item.count

          })

          return c

        },

        getGoodsCount(state){  //将商品id作为键和对应的count作为值放在一个对象中

          var o = { }

          state.car.forEach(item => {

            o[item.id] = item.count

          })

          return o

        },

        getSelected(state){  ///将商品id作为键和对应的选择状态selectStatus作为值放在一个对象中

          var o = {}

          state.car.forEach(item => {

            o[item.id] = item.selectStatus

          })

          return o

        },

        getGoodsCountAndAmount(state){

          var o  = {
            
            count : 0,

            amount : 0

          }

          state.car.forEach(item=> {

            if(item.selectStatus){
              
              o.count += item.count

              o.amount += item.count * item.price

            }

          })

          return o

        }

    }

})

//=========vue-resource ============
import VueResource from 'vue-resource'

Vue.use(VueResource)

Vue.http.options.root = "http://www.liulongbin.top:3005" //设置全局接口根路径

Vue.http.options.emulateJSON = true //设置post表单全局组织样式


//=========设置懒加载============
import VueLazyload from 'vue-lazyload'

Vue.use(VueLazyload, {
  preLoad: 1.3,
  loading: './lib/images/loading.gif',
  attempt: 1
})

//=========设置缩略图============
import VuePreview from 'vue-preview'

// Vue.use(VuePreview)

// with parameters install
Vue.use(VuePreview, {
  mainClass: 'pswp--minimal--dark',
  barsSize: {top: 0, bottom: 0},
  captionEl: false,
  fullscreenEl: false,
  shareEl: false,
  bgOpacity: 0.85,
  tapToClose: true,
  tapToToggleControls: false
})

//=========设置全局过滤器============
import moment from 'moment'

Vue.filter('dateFormat', function(dataStr ,pattern = "YYYY-MM-DD HH:mm:ss"){

    return moment(dataStr).format(pattern)

})


//=========element ui ============
import ElementUI from 'element-ui';

import 'element-ui/lib/theme-chalk/index.css';

Vue.use(ElementUI);

//==========bootstrap ============
import 'bootstrap/dist/css/bootstrap.css'

//==========mui ============
import './lib/mui-master/dist/css/mui.css'
import './lib/mui-master/dist/css/icons-extra.css'

import app from './app.vue'


var vm = new Vue({
    el: '#app',
    render: c => c(app),
    router, //挂载路由
    store //挂载vuex
})